﻿namespace BingMapsLocationDemo
{
    using System;
    using System.Reactive.Linq;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using WP7Contrib.Communications;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.BingMaps;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly ILog log = new DebugLog();
        private readonly IBingMapsService bingMapsService = null;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.log = new DebugLog();
            this.bingMapsService = new BingMapsService(new ResourceClientFactory(this.log),
                                                       new UrlEncoder(),
                                                       new Settings("Yuor credentials id", "Your app id", 10000, 5));
        }

        private void getAddress_Click(object sender, RoutedEventArgs e)
        {
            var criterion = CriterionFactory.CreateLocationSearchForAddress(this.postCode.Text);

            this.bingMapsService.SearchForLocationUsingAddress(criterion)
                .ObserveOnDispatcher()
                .Subscribe(result =>
                               {
                                   this.address.Text = result.Locations[0].Address.Locality;
                                   this.address.Text += Environment.NewLine;
                                   this.address.Text += result.Locations[0].Address.PostalCode;
                                   this.address.Text += Environment.NewLine;
                                   this.address.Text += result.Locations[0].Address.AdminDistrict;
                                   this.address.Text += Environment.NewLine;
                                   this.address.Text += result.Locations[0].Address.CountryRegion;
                               });
        }
    }
}